package com.meida.module.bus.provider.handler;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.meida.common.base.module.ExportField;
import com.meida.common.base.module.MyExportParams;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.ExportInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusReportOuttwo;
import com.meida.module.bus.provider.service.BusReportOuttwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("busReportOutTwoExportHandler")
public class BusReportOutTwoExportHandler implements ExportInterceptor<BusReportOuttwo> {

    @Autowired
    BusReportOuttwoService busReportOuttwoService;


    @Override
    public void initExportParams(MyExportParams exportParams) {

    }

    @Override
    public void initExportFieldList(List<ExportField> exportFieldList) {

    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List<ExcelExportEntity> entityList) {
        String key = (String) exportField.getKey();
        String name = exportField.getName();
        if (FlymeUtils.isEmpty(key)) {
            List<ExportField> children = exportField.getChildren();
            List<ExcelExportEntity> excelExportEntityList = new ArrayList<>();
            for (ExportField childrenExportField : children) {
                childrenExportField.setGroupName(name);
                if ("合同造价".equals(name)) {
                    exportField.setKey("a");
                }
                if ("截止上月已完工程量".equals(name)) {
                    exportField.setKey("b");
                }
                if ("本月完成量".equals(name)) {
                    exportField.setKey("c");
                }
                if ("截止本期累计".equals(name)) {
                    exportField.setKey("d");
                }
                excelExportEntityList.add(new ExcelExportEntity(childrenExportField.getName(), childrenExportField.getKey()));
            }
            exportField.setList(excelExportEntityList);
            exportField.setChildren(new ArrayList<>());
        }

    }

    @Override
    public ResultBody initData(Map params) {
        ResultBody resultBody = new ResultBody();
        List<EntityMap> busReportOutTwoList = busReportOuttwoService.getExportData(params);
        List<EntityMap> list = new ArrayList();
        if (FlymeUtils.isEmpty(busReportOutTwoList)) {
            list.add(new EntityMap());
            resultBody.data(list);
        } else {
            resultBody.data(busReportOutTwoList);
        }
        for (EntityMap entityMap : busReportOutTwoList) {

            List<EntityMap> list1 = new ArrayList<>();
            EntityMap entityMap1 = new EntityMap();
            entityMap1.put("contractSyntheticalPrice", entityMap.getBigDecimal("contractSyntheticalPrice"));
            entityMap1.put("contractCostSynthetical", entityMap.getBigDecimal("contractCostSynthetical"));
            list1.add(entityMap1);
            entityMap.put("a", list1);

            List<EntityMap> list2 = new ArrayList<>();
            EntityMap entityMap2 = new EntityMap();
            entityMap2.put("lastMonthFinshNum", entityMap.getBigDecimal("lastMonthFinshNum"));
            entityMap2.put("lastMonthFinshCost", entityMap.getBigDecimal("lastMonthFinshCost"));
            entityMap2.put("lastMonthFinshPercentage", entityMap.getBigDecimal("lastMonthFinshPercentage"));
            list2.add(entityMap2);
            entityMap.put("b", list2);

            List<EntityMap> list3 = new ArrayList<>();
            EntityMap entityMap3 = new EntityMap();
            entityMap3.put("nowMonthFinshNum", entityMap.getBigDecimal("nowMonthFinshNum"));
            entityMap3.put("nowMonthFinshCost", entityMap.getBigDecimal("nowMonthFinshCost"));
            entityMap3.put("nowMonthFinshPercentage", entityMap.getBigDecimal("nowMonthFinshPercentage"));
            list3.add(entityMap3);
            entityMap.put("c", list3);

            List<EntityMap> list4 = new ArrayList<>();
            EntityMap entityMap4 = new EntityMap();
            entityMap4.put("nowPeriodSumNum", entityMap.getBigDecimal("nowPeriodSumNum"));
            entityMap4.put("nowPeriodSumCost", entityMap.getBigDecimal("nowPeriodSumCost"));
            entityMap4.put("nowPeriodPercentage", entityMap.getBigDecimal("nowPeriodPercentage"));
            list4.add(entityMap4);
            entityMap.put("d", list4);
        }
        return resultBody;
    }
}

